# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):
    def invertTree(self, root):
        """
        :type root: TreeNode
        :rtype: TreeNode
        """
        q = [root]
        while q:
            node = q.pop(0)
            if not node:
                continue
            tmp = None
            if node.left:
                tmp = node.left
            node.left = node.right
            node.right = tmp
            q.append(node.left)
            q.append(node.right)
        return root
